<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  입수금
**************************************************************************** */

class MoneyM extends N_Model {

	/***************************************************
	 * preprocess
	 *     constructor
	 *     function
	 *     struct and global values
	 ***************************************************/
	var $tableName;
    function __construct() {
        parent::__construct();
		$this->tableName = 'tbl_item_money_characters';
    }


	/***************************************************
	 * c
	 ***************************************************/

	# 생성
	function c01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			INSERT INTO
				tbl_item_money_characters
			SET %1$s;',
			$sqlSetStatment
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * r
	 ***************************************************/

	# 목록 조회
	function r01($argArray = array()){
		# 상세
		$whereStatement01 = '';
		if(isset($argArray['pk_uid'])===TRUE){
			$whereStatement01 = sprintf('
				 WHERE pk_uid = %s',
				 $this->db->escape_str($argArray['pk_uid'])
			);
		}

		# 검색
		$whereStatement02 = '';
		$whereKey = '';
		$whereVal = '';
		if(isset($argArray['where'])===TRUE){
			$whereKey = $argArray['where']['whereKey'];
			$whereVal = $argArray['where']['whereVal'];
			if($argArray['where']['whereOpt'] == 'absolute'){
				$whereStatement02 = sprintf('
					WHERE %s = "%s" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			} else {
				$whereStatement02 = sprintf('
					WHERE %s LIKE "%%%s%%" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			}
		}
		//nlog($whereStatement02);

		# 페이지네이션
		$limitStatement = '';
		if(isset($argArray['paginationCfg']['paginationFirst']) === TRUE && isset($argArray['paginationCfg']['paginationSecond']) === TRUE){
			$limitStatement = sprintf('
				 LIMIT %s, %s',
				 $argArray['paginationCfg']['paginationFirst'], $argArray['paginationCfg']['paginationSecond']
			);
		}
		$orderByStatement = ' ORDER BY dt_flowing DESC ';

		# 쿼리
		$sql = sprintf('
			SELECT
				pk_uid,
				(SELECT
					tbl_characters.uq_name
				FROM
					tbl_characters
				WHERE tbl_item_money_characters.fk_characters_uid = tbl_characters.pk_uid) AS characters_name,
				fk_characters_uid,
				fg_flow,
				(SELECT
					tbl_enum.text
				FROM
					tbl_enum
				WHERE tbl_item_money_characters.fg_flow = tbl_enum.code) AS fg_flow_kr,
				no_money,
				(SELECT
					CONCAT(
						tbl_item_money_characters.dt_flowing,
						" ",
						tbl_item_money_characters.tm_flowing
					)) AS flowing,
				dt_flowing,
				tm_flowing,
				tx_description
			FROM
				tbl_item_money_characters
			%s
			%s
			%s
			%s
			;',
			$whereStatement01,
			$whereStatement02,
			$orderByStatement,
			$limitStatement
		);
		//nlog($sql);

		# 쿼리실행
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		if(isset($argArray['pk_uid'])===TRUE){
			$retVal = @$result[0];
		} else {
			$retVal = $result;
		}
		return $retVal;
	}

	# 목록 카운트
	function r02($argArray = array()){
		# 검색조건
		$whereStatement02 = '';
		$whereKey = '';
		$whereVal = '';
		nlog('model argArray', print_r($argArray,true));
		if(isset($argArray['where'])===TRUE){
			$whereKey = $argArray['where']['whereKey'];
			$whereVal = $argArray['where']['whereVal'];
			if($argArray['where']['whereOpt'] == 'absolute'){
				$whereStatement02 = sprintf('
					WHERE %s = "%s" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			} else {
				$whereStatement02 = sprintf('
					WHERE %s LIKE "%%%s%%" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			}
		}
		//nlog('r02where>>>'+$whereStatement02);

		# 쿼리
		$sql = sprintf('
			SELECT COUNT(pk_uid) AS cnt FROM tbl_item_money_characters %1$s',
			$whereStatement02
		);
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = @$result[0]['cnt'];
		return $retVal;
	}

	# FK를 기준으로 입수금 조회
	function r03($argArray = array()){
		$sql = sprintf('
			SELECT
				# 입수금
				IFNULL((SELECT
					SUM(tbl_item_money_characters.no_money) AS gain_money_sum
				FROM
					tbl_item_money_characters
				WHERE tbl_item_money_characters.fk_characters_uid = "%1$s"
					AND fg_flow = "gain"),0) -
				IFNULL((SELECT
					SUM(tbl_item_money_characters.no_money) AS lose_money_sum
				FROM
					tbl_item_money_characters
				WHERE tbl_item_money_characters.fk_characters_uid = "%1$s"
					AND fg_flow = "lose"),0) AS no_money;',
			$argArray['fk_characters_uid']
		);
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = @$result[0]['no_money'];
		if($retVal <= 0){
			$retVal = 0;
		}
		return $retVal;
	}

	# 정산이 가능한 캐릭터만 리턴
	function r04($argArray = array()){
		$allCharacters = $this->GlobalM->getMembersCharactersList();

		$result = $this->db->query(sprintf('SELECT fk_characters_uid FROM tbl_item_money_characters GROUP BY fk_characters_uid;'))->result_array();
		//print_r($ableCalcutateCharacterUid);
		$ableCalcutateCharacterUid = array();
		foreach($result as $key=>$val){
			array_push($ableCalcutateCharacterUid, $val['fk_characters_uid']);
		}
		//print_r($resultArray);
		foreach($allCharacters as $key=>$val){
			foreach($val['items'] as $k=>$v){
				if(!in_array($k, $ableCalcutateCharacterUid)){
					//echo $k . PHP_EOL;
					unset($allCharacters[$key]['items'][$k]);
				}
			}
		}
		//nlog($allCharacters);
		$retVal = $allCharacters;
		return $retVal;
	}


	/***************************************************
	 * u
	 ***************************************************/

	# PK를 기준으로 업데이트
	function u01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			UPDATE
				tbl_item_money_characters
			SET
				%1$s
			WHERE
				pk_uid = %2$s;',
			$sqlSetStatment,
			$this->db->escape_str($argArray['pk_uid'])
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}

	//	# FK를 기준으로 최종 로그인 시간 업데이트
	//	function u02($argArray = array()){
	//		$sqlSetStatment = '';
	//		$sql = sprintf('
	//			UPDATE tbl_item_money_characters SET
	//				dt_last_logined = "%1$s",
	//				tm_last_logined = "%2$s"
	//			WHERE
	//				uq_member_id = "%3$s"',
	//			$argArray['dt_last_logined'],
	//			$argArray['tm_last_logined'],
	//			$argArray['uq_member_id']
	//		);
	//
	//		$this->db->trans_start();
	//		$this->db->query($sql, $argArray);
	//		$this->db->trans_complete();
	//		if ($this->db->trans_status() === TRUE) {
	//			$result = TRUE;
	//		}
	//		if($result){
	//			$retVal = TRUE;
	//		} else {
	//			$retVal = FALSE;
	//		}
	//		return $retVal;
	//	}


	/***************************************************
	 * d
	 ***************************************************/

	# PK를 기준으로 삭제
	function d01($argArray = array()){
		if(empty($argArray['pk_uid']) !== FALSE){
			return FALSE;
		}
		$sql = sprintf('
			DELETE FROM
				tbl_item_money_characters
			WHERE
				pk_uid = %1$s;',
			$this->db->escape_str(@$argArray['pk_uid'])
		);
		nlog($sql);
		$query = $this->db->query($sql);
		$result = $this->db->affected_rows();
		if($result > 0){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}

	# FK를 기준으로 삭제
	function d02($argArray = array()){
		if(empty($argArray['fk_characters_uid']) !== FALSE){
			return FALSE;
		}
		$sql = sprintf('
			DELETE FROM
				tbl_item_money_characters
			WHERE
				fk_characters_uid = %1$s;',
			$this->db->escape_str(@$argArray['fk_characters_uid'])
		);
		//nlog($sql);
		$query = $this->db->query($sql);
		$result = $this->db->affected_rows();
		if($result > 0){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


}
/* End of file */